package com.ruoyi.product.controller;


import com.ruoyi.product.Service.ProductService;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("activiti")
@RestController
public class ActivitiController {


	@Resource
	ProductService productService;

	/**
	 * 部署任务
	 */
	@RequestMapping("deploy")
	public static void deploy() {
		// 1、创建ProcessEngine
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		// 2、得到RepositoryService实例
		RepositoryService repositoryService = processEngine.getRepositoryService();
		// 3、使用RepositoryService进行部署
		Deployment deployment = repositoryService.createDeployment()
				// 添加bpmn资源
				.addClasspathResource("processes/Leave.bpmn20.xml")
				// 添加png资源
				.addClasspathResource("processes/Leave.png")
				.name("请假申请流程")
				.deploy();
		System.out.println("流程部署id：" + deployment.getId());
		System.out.println("流程部署名称：" + deployment.getName());
		System.out.println("流程部署key"+ deployment.getKey());
		System.out.println(""+deployment.getTenantId());
		System.out.println(""+deployment.getProjectReleaseVersion());


	}


	/**
	 * 启动流程的时候设置流程变量
	 */
	@RequestMapping("start")
	public static String testStartProcessAndSetVariables(String assignee) {
		// 1、获取流程引擎
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		// 2、获取RunTimeService
		RuntimeService runtimeService = processEngine.getRuntimeService();
		// 3、流程定义的Key
		String key = "Leave";
		// 4、流程变量的map
		Map<String, Object> variables = new HashMap<>();
		// 8、设定任务的负责人
		variables.put("employee", assignee);
		variables.put("TeamLeader", "组长");
		variables.put("Manager", "经理");
		// 9、启动流程
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, variables );
		System.out.println(processInstance.getProcessInstanceId());
		return  processInstance.getProcessInstanceId();

	}

	/**
	 * 查询当前个人待执行的任务
	 */
	@RequestMapping("testFindPersonalTaskList")
	public static List<String> testFindPersonalTaskList(String assignee) throws Exception {
		// 任务负责人
// String assignee = "employee";
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		// 创建TaskService
		TaskService taskService = processEngine.getTaskService();
		// 根据流程key和任务负责人查询任务
		List<Task> list = taskService.createTaskQuery()
				//流程Key
				.processDefinitionKey("Leave")

				//只查询该任务负责人的任务
         .taskAssignee(assignee)
				.list();
		List<String> strings = new ArrayList<>();
		for (Task task : list) {
			System.out.println("流程实例id：" + task.getProcessInstanceId());
			System.out.println("任务id：" + task.getId());
			System.out.println("任务负责人：" + task.getAssignee());
			System.out.println("任务名称：" + task.getName());
			strings.add(task.getProcessInstanceId());
		}

		return strings ;
	}

	/**
	 * 完成个人任务
	 *
	 * @param
	 */
	@RequestMapping("completTaskByVariables")
	public static String completTaskByVariables(String pid) {
		// 1、流程定义的Key
		String key = "Leave";
		// 2、获取流程引擎
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		// 3、获取taskService
		TaskService taskService = processEngine.getTaskService();
		// 4、查询任务
		Task task = taskService.createTaskQuery()
				.processDefinitionKey(key)
//				.taskAssignee(assingee)
				//产品id
				.processInstanceId(pid)
				.singleResult();
		if (task != null) {
			taskService.complete(task.getId());
			System.out.println(task.getId() + "----任务负责人：" + task.getAssignee() + "----任务已完成");
			return   task.getAssignee() ;
		}
		return "任务失败";
	}


}
